<?php
namespace application\datatypes;
use application\datatypes\DataType;
use application\utilities\Utility;
use application\system\Permalink as Permalink;

class Tish extends DataType {
	public static $name = 'Tish';
	public static $fields = array(array('columnType'=>'LONGTEXT'));
	
	public static function formJS($field) {
		Modulator()->includeScript('jquery');
		Modulator()->includeScript(CN_URL.'modules/tish/jquery-linedtextarea.js');
		Modulator()->includeScript(CN_URL.'modules/tish/insert-at-caret.js');
		?><link rel="stylesheet" type="text/css" media="all" href="<?= CN_URL?>modules/tish/jquery-linedtextarea.css" />
		<script type="text/javascript">
			$(document).ready(function() {
				$('#<?=$field->cf_column?>').linedtextarea();
				$('#<?=$field->cf_column?>').keydown(function(event) {
					if(event.keyCode == '9') {
						event.preventDefault();
						insertAtCaret('<?=$field->cf_column?>', "\t");
						
					}
					
				});
			});
		</script>
		<?php
		
	}
	
	public static function displayJS($field) {
		Modulator()->includeScript('jquery');
		Modulator()->includeScript(CN_URL.'modules/tish/tish.jquery.js');
		?><link rel="stylesheet" type="text/css" media="all" href="<?= CN_URL?>modules/tish/default.css" />
		<script type="text/javascript">
			$(document).ready(function() {
				$('#<?=$field->cf_column?>').tish();
			});
		</script>
		<?php
	}
	
	public static function validate($field, $data, $results) {
		if($field->cf_required && !$data[$field->cf_column]) {
			$results['errors'][] = '<b>'.$field->cf_display_name.'</b> is required.';
		}
		elseif($value = $data[$field->cf_column]) {
			$results['db_fields'][$field->cf_column] = $value;	
		}
		
		return $results;
	}
	
	public static function display($formElements) {
		?>
		<fieldset class="single-field">
			<legend><?=$formElements['display']?></legend>
			<?php if($description = $formElements['description']) : ?>
				<p class="form-description"><?=$description?></p>
			<?php endif;?>
			<?=$formElements['elements']['element']?>
		</fieldset>
		<?php
	}
	
	public static function build($field, $data) {
		$value = html_entity_decode($data[$field->cf_column]);
		$formElements = array('description'=>$field->cf_description, 'column'=>$field->cf_column, 'display'=>$field->cf_display_name, 'data_type'=>$field->cf_data_type, 'elements'=>array(
						'element'=>
							sprintf('<textarea class="code" name="%1$s" id="%1$s">%2$s</textarea>', $field->cf_column, $value)
						
		));
		return $formElements;
	
	}
	
	public static function prepareData($field, $data, $params) {
		require CN_DOCUMENT_ROOT.'modules/tish/TishHighlighter.php';
		$tish = new \TishHighlighter;
		$tish->setFunctionReplaceCallback(function($function) {
			$id = DB()->getVar("SELECT `content_id` FROM `cn_content` WHERE `content_type`='6' AND `content_title` LIKE '$function'");
			if($id) {
				return '<span class="concerto-function"><a href="'.Permalink::buildPermalink('content', $id).'">'.$function.'</a></span>';
			}
			else {
				return '<span class="function">'.$function.'</span>';
			}
		});
		ob_start();
		$tish->parse(stripslashes($data[$field->cf_column]));
		$html = ob_get_clean();
		return '<pre id="'.$field->cf_column.'" class="tish"><div class="tish-inner">'.$html.'</div></pre>'.'<div style="clear:both;"></div>';
	}
	
	public static function editForm($field) {
		?>
		<div class="box-content">
			<form method="post">
			<input type="hidden" name="field_id" value="<?=$field->getId()?>"/>
			<p>
			<label>Field Display Name</label>
			<input type="text" name="field_display_name" class="big wide" value="<?=$field->getDisplayName()?>"/>
			</p>
			<p>
			<label>Field Description</label>
			<textarea name="field_description" class="wide"><?=$field->getDescription()?></textarea>
			</p>
			<p>
				<label><input type="checkbox" name="field_required" <?=($field->getRequired()) ? 'checked="checked"' : ''?> value="1"/> Required</label>
				<label><input type="checkbox" name="field_show" value="1" <?=($field->getShow()) ? 'checked="checked"' : ''?>/> Show in Manage Panel</label>
				
			</p>
			
			
			<p><input type="submit" name="update_field" class="small-button" value="Submit Changes"/></p>
			</form>
		</div>
			<?php
	}
	
}
?>